diff --git a/tools/sync-registry-secrets.py b/tools/sync-registry-secrets.py new file mode 100644 index 0000000..84a3d64 --- /dev/null +++ b/tools/sync-registry-secrets.py @@ -0,0 +1,172 @@ +#!/usr/bin/env python3 +""" +sync-registry-secrets: propagate REGISTRY_USERNAME / REGISTRY_TOKEN (and +REGISTRY_PASSWORD) secrets to every Gitea repo that uses them in its Actions +workflows. + +For every repo returned by GET /api/v1/repos/search (paginated) we inspect +.gitea/workflows/*.yml and .github/workflows/*.yml via the raw content API. +Workflows that reference secrets.REGISTRY_USERNAME, REGISTRY_TOKEN, or +REGISTRY_PASSWORD (and don't already have those secrets set on the repo) +get the secret PUT via /api/v1/repos/{owner}/{repo}/actions/secrets/{name}. + +SECRETS_SYNCER_TOKEN env is required and must have admin-level write access +to target repos and orgs. +""" +import json +import os +import sys +import urllib.error +import urllib.request +from pathlib import Path + +API = os.environ.get("GITEA_API", "https://gitea.rudolf.keenetic.link/api/v1").rstrip("/") +TOKEN = os.environ.get("SECRETS_SYNCER_TOKEN", "") +DRY_RUN = os.environ.get("DRY_RUN", "0") == "1" +LIMIT = int(os.environ.get("REPO_LIMIT", "200")) +SECRET_NAMES = ("REGISTRY_USERNAME", "REGISTRY_TOKEN", "REGISTRY_PASSWORD") + +SECRET_VALUES = {} + + +def http(method, url, body=None): + data = None + headers = {"Authorization": f"token {TOKEN}"} + if body is not None: + data = json.dumps(body).encode() + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=30) as r: + return r.status, r.read().decode("utf-8", errors="replace") + except urllib.error.HTTPError as e: + return e.code, e.read().decode("utf-8", errors="replace") + except Exception as e: + return 0, str(e) + + +def list_my_repos(limit): + out = [] + page = 1 + while len(out) < limit: + url = f"{API}/repos/search?limit=50&page={page}" + status, body = http("GET", url) + if status != 200: + break + data = json.loads(body) + page_data = data.get("data", []) + if not page_data: + break + out.extend(page_data) + page += 1 + return out[:limit] + + +def list_workflow_files(full_name): + files = [] + for prefix in (".gitea/workflows", ".github/workflows"): + url = f"{API}/repos/{full_name}/contents/{prefix}?ref=main" + status, body = http("GET", url) + if status != 200: + for ref in ("master", "default"): + url = f"{API}/repos/{full_name}/contents/{prefix}?ref={ref}" + status, body = http("GET", url) + if status == 200: + break + else: + continue + try: + entries = json.loads(body) + except json.JSONDecodeError: + continue + if not isinstance(entries, list): + continue + for entry in entries: + name = entry.get("name", "") + if name.endswith((".yml", ".yaml")): + files.append((prefix + "/" + name, entry.get("sha"))) + return files + + +def workflow_uses_secret(full_name, path, secret_name): + url = f"{API}/repos/{full_name}/raw/main/{path}" + status, body = http("GET", url) + if status != 200: + return False + needle = f"secrets.{secret_name}" + return needle in body + + +def secret_exists(full_name, secret_name): + url = f"{API}/repos/{full_name}/actions/secrets/{secret_name}" + status, _ = http("GET", url) + return status == 200 + + +def put_secret(full_name, secret_name, value): + url = f"{API}/repos/{full_name}/actions/secrets/{secret_name}" + if DRY_RUN: + return 0, "dry-run" + return http("PUT", url, body={"value": value}) + + +def main(): + if not TOKEN: + print("SECRETS_SYNCER_TOKEN not set", file=sys.stderr) + return 1 + SECRET_VALUES["REGISTRY_USERNAME"] = os.environ.get("REGISTRY_USERNAME_VALUE", "") + SECRET_VALUES["REGISTRY_TOKEN"] = os.environ.get("REGISTRY_TOKEN_VALUE", "") + SECRET_VALUES["REGISTRY_PASSWORD"] = os.environ.get("REGISTRY_PASSWORD_VALUE", "") + if not any(SECRET_VALUES.values()): + print("no REGISTRY_*_VALUE env vars set", file=sys.stderr) + return 1 + + repos = list_my_repos(LIMIT) + print(f"scanning {len(repos)} repos", file=sys.stderr) + + synced = [] + skipped = [] + failed = [] + + for r in repos: + full_name = r.get("full_name") + if not full_name: + continue + wf_files = list_workflow_files(full_name) + if not wf_files: + continue + referenced = set() + for path, _ in wf_files: + for sn in SECRET_NAMES: + if workflow_uses_secret(full_name, path, sn): + referenced.add(sn) + if not referenced: + continue + for sn in sorted(referenced): + value = SECRET_VALUES.get(sn) + if not value: + skipped.append((full_name, sn, "no value in env")) + continue + if secret_exists(full_name, sn): + skipped.append((full_name, sn, "already present")) + continue + status, body = put_secret(full_name, sn, value) + if status in (200, 201, 204): + synced.append((full_name, sn, status)) + else: + failed.append((full_name, sn, f"{status}: {body[:120]}")) + + print(f"synced: {len(synced)}") + for s in synced: + print(f" + {s[0]} {s[1]} ({s[2]})") + print(f"skipped: {len(skipped)}") + for s in skipped: + print(f" . {s[0]} {s[1]} ({s[2]})") + print(f"failed: {len(failed)}") + for f in failed: + print(f" ! {f[0]} {f[1]} ({f[2]})") + return 0 if not failed else 2 + + +if __name__ == "__main__": + sys.exit(main())