feat(tools): add admin self-check to sync-registry-secrets
This commit is contained in:
@@ -45,6 +45,25 @@ def http(method, url, body=None):
|
||||
return 0, str(e)
|
||||
|
||||
|
||||
def is_admin_self_check():
|
||||
"""Probe admin endpoints to validate token scope."""
|
||||
probes = (
|
||||
(f"{API}/user", "self"),
|
||||
(f"{API}/admin/runners", "admin runners list"),
|
||||
(f"{API}/repos/hermes/ansible/actions/secrets/REGISTRY_USERNAME", "existing secret on others"),
|
||||
)
|
||||
print("--- admin self-check ---", file=sys.stderr)
|
||||
for url, hint in probes:
|
||||
status, body = http("GET", url)
|
||||
print(f" probe {url}: status={status} body[:200]={body[:200]!r}", file=sys.stderr)
|
||||
admin_ok = True
|
||||
for url, _hint in probes[1:]:
|
||||
s, _b = http("GET", url)
|
||||
if s in (401, 403):
|
||||
admin_ok = False
|
||||
return admin_ok
|
||||
|
||||
|
||||
def list_my_repos(limit):
|
||||
out = []
|
||||
page = 1
|
||||
@@ -99,8 +118,14 @@ def workflow_uses_secret(full_name, path, secret_name):
|
||||
|
||||
def secret_exists(full_name, secret_name):
|
||||
url = f"{API}/repos/{full_name}/actions/secrets/{secret_name}"
|
||||
status, _ = http("GET", url)
|
||||
return status == 200
|
||||
status, body = http("GET", url)
|
||||
if status == 200:
|
||||
return True
|
||||
if "Required" in body:
|
||||
return False
|
||||
if status in (404, 422):
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def put_secret(full_name, secret_name, value):
|
||||
@@ -114,6 +139,7 @@ def main():
|
||||
if not TOKEN:
|
||||
print("SECRETS_SYNCER_TOKEN not set", file=sys.stderr)
|
||||
return 1
|
||||
is_admin_self_check()
|
||||
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", "")
|
||||
@@ -154,7 +180,7 @@ def main():
|
||||
if status in (200, 201, 204):
|
||||
synced.append((full_name, sn, status))
|
||||
else:
|
||||
failed.append((full_name, sn, f"{status}: {body[:120]}"))
|
||||
failed.append((full_name, sn, f"{status}: {body[:200]}"))
|
||||
|
||||
print(f"synced: {len(synced)}")
|
||||
for s in synced:
|
||||
|
||||
Reference in New Issue
Block a user