196 lines
6.5 KiB
Python
196 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
sync-registry-secrets: verbose-mode version with full-branch scan.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
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_repo_branches(full_name):
|
|
branches = []
|
|
page = 1
|
|
while True:
|
|
url = f"{API}/repos/{full_name}/branches?limit=50&page={page}"
|
|
status, body = http("GET", url)
|
|
if status != 200:
|
|
break
|
|
try:
|
|
arr = json.loads(body)
|
|
except json.JSONDecodeError:
|
|
break
|
|
if not arr:
|
|
break
|
|
branches.extend([b["name"] for b in arr if isinstance(b, dict) and "name" in b])
|
|
if len(arr) < 50:
|
|
break
|
|
page += 1
|
|
return branches
|
|
|
|
|
|
def list_workflow_files(full_name):
|
|
files = []
|
|
seen = set()
|
|
branches_to_check = list_repo_branches(full_name)
|
|
branches_to_check = [b for b in branches_to_check if b not in seen and not seen.add(b)]
|
|
if not branches_to_check:
|
|
try:
|
|
r = http("GET", f"{API}/repos/{full_name}")
|
|
if isinstance(r, tuple) and r[0] == 200:
|
|
data = json.loads(r[1])
|
|
if isinstance(data, dict) and data.get("default_branch"):
|
|
branches_to_check.append(data["default_branch"])
|
|
except Exception:
|
|
pass
|
|
if not branches_to_check:
|
|
branches_to_check = ["main", "master", "default"]
|
|
branches_to_check = branches_to_check[:25]
|
|
for prefix in (".gitea/workflows", ".github/workflows"):
|
|
for branch in branches_to_check:
|
|
url = f"{API}/repos/{full_name}/contents/{prefix}?ref={branch}"
|
|
status, body = http("GET", url)
|
|
if status != 200:
|
|
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, ref):
|
|
for branch in ("main", "master", "default", ref):
|
|
url = f"{API}/repos/{full_name}/raw/{branch}/{path}"
|
|
status, body = http("GET", url)
|
|
if status == 200:
|
|
needle = f"secrets.{ref}"
|
|
return needle in body
|
|
return False
|
|
|
|
|
|
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={"data": 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", "")
|
|
|
|
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:
|
|
print(f"skip {full_name}: no workflow files", file=sys.stderr)
|
|
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:
|
|
print(f"skip {full_name}: no REGISTRY_* refs (wf_files={len(wf_files)})", file=sys.stderr)
|
|
continue
|
|
print(f"{full_name}: needs {sorted(referenced)}", file=sys.stderr)
|
|
for sn in sorted(referenced):
|
|
value = SECRET_VALUES.get(sn)
|
|
if not value:
|
|
print(f" skip {sn}: no value in env", file=sys.stderr)
|
|
skipped.append((full_name, sn, "no value in env"))
|
|
continue
|
|
if secret_exists(full_name, sn):
|
|
print(f" skip {sn}: already present", file=sys.stderr)
|
|
skipped.append((full_name, sn, "already present"))
|
|
continue
|
|
status, body = put_secret(full_name, sn, value)
|
|
print(f" PUT {sn}: status={status} body[:200]={body[:200]!r}", file=sys.stderr)
|
|
if status in (200, 201, 204):
|
|
synced.append((full_name, sn, status))
|
|
else:
|
|
failed.append((full_name, sn, f"{status}: {body[:200]}"))
|
|
|
|
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())
|