|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import http.cookiejar |
| 4 | +import json |
| 5 | +import os |
| 6 | +import ssl |
| 7 | +import sys |
| 8 | +import time |
| 9 | +import urllib.error |
| 10 | +import urllib.parse |
| 11 | +import urllib.request |
| 12 | + |
| 13 | +# Configuration |
| 14 | +QUAY_HOST = os.getenv("QUAY_HOST") |
| 15 | +USERNAME = os.getenv("QUAY_ADMIN_USER", "username") |
| 16 | +EMAIL = os.getenv("QUAY_ADMIN_EMAIL", "user@example.com") |
| 17 | +PASSWORD = os.getenv("QUAY_ADMIN_PASSWORD") |
| 18 | +CA_CERT = os.getenv("CA_CERT", "/run/secrets/kubernetes.io/serviceaccount/ca.crt") |
| 19 | + |
| 20 | +if not all([QUAY_HOST, PASSWORD]): |
| 21 | + print("ERROR: Missing QUAY_HOST or QUAY_ADMIN_PASSWORD env vars") |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | +BASE_URL = f"https://{QUAY_HOST}" |
| 25 | + |
| 26 | + |
| 27 | +def log(msg): |
| 28 | + """Log a message to the console""" |
| 29 | + print(f"[{time.strftime('%X')}] {msg}", flush=True) |
| 30 | + |
| 31 | + |
| 32 | +# Setup SSL |
| 33 | +ctx = ssl.create_default_context() |
| 34 | +if os.path.exists(CA_CERT): |
| 35 | + ctx.load_verify_locations(CA_CERT) |
| 36 | + log(f"Using CA certificate from {CA_CERT}") |
| 37 | + ctx.check_hostname = True |
| 38 | + ctx.verify_mode = ssl.CERT_REQUIRED |
| 39 | +else: |
| 40 | + log(f"WARNING: CA certificate file not found at {CA_CERT}") |
| 41 | + ctx.check_hostname = False |
| 42 | + ctx.verify_mode = ssl.CERT_NONE |
| 43 | + |
| 44 | +# Setup Cookies (Required for CSRF) |
| 45 | +cj = http.cookiejar.CookieJar() |
| 46 | +opener = urllib.request.build_opener( |
| 47 | + urllib.request.HTTPSHandler(context=ctx), |
| 48 | + urllib.request.HTTPCookieProcessor(cj), |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +def wait_for_quay(): |
| 53 | + """Loop until Quay health endpoint returns 200""" |
| 54 | + url = f"{BASE_URL}/health/instance" |
| 55 | + while True: |
| 56 | + try: |
| 57 | + log(f"Checking Quay health at {url}...") |
| 58 | + with opener.open(url, timeout=10) as response: |
| 59 | + if response.status == 200: |
| 60 | + log("Quay is Online.") |
| 61 | + return |
| 62 | + except Exception as e: |
| 63 | + log(f"Quay unavailable ({e}). Retrying in 5s...") |
| 64 | + time.sleep(5) |
| 65 | + |
| 66 | + |
| 67 | +def get_csrf_token(): |
| 68 | + """Fetch CSRF token and prime the cookie jar""" |
| 69 | + url = f"{BASE_URL}/csrf_token" |
| 70 | + with opener.open(url) as response: |
| 71 | + data = json.loads(response.read().decode()) |
| 72 | + token = data.get("csrf_token") |
| 73 | + return token |
| 74 | + |
| 75 | + |
| 76 | +def create_user(): |
| 77 | + """Perform the creation flow""" |
| 78 | + try: |
| 79 | + log("Attempting to create user...") |
| 80 | + csrf_token = get_csrf_token() |
| 81 | + |
| 82 | + url = f"{BASE_URL}/api/v1/user/" |
| 83 | + payload = json.dumps( |
| 84 | + { |
| 85 | + "username": USERNAME, |
| 86 | + "email": EMAIL, |
| 87 | + "password": PASSWORD, |
| 88 | + "_csrf_token": csrf_token, |
| 89 | + } |
| 90 | + ).encode("utf-8") |
| 91 | + |
| 92 | + headers = { |
| 93 | + "Content-Type": "application/json", |
| 94 | + "X-CSRF-Token": csrf_token, |
| 95 | + } |
| 96 | + |
| 97 | + req = urllib.request.Request(url, data=payload, headers=headers, method="POST") |
| 98 | + |
| 99 | + with opener.open(req) as response: |
| 100 | + if response.status in [200, 201, 202]: |
| 101 | + log("SUCCESS: User created successfully.") |
| 102 | + return True |
| 103 | + except urllib.error.HTTPError as e: |
| 104 | + if e.code == 400: |
| 105 | + log(f"User '{USERNAME}' already exists. Exiting.") |
| 106 | + return True |
| 107 | + log(f"FAILED to create user: {e.code} {e.reason}") |
| 108 | + except Exception as e: |
| 109 | + log(f"FAILED to create user: {e}") |
| 110 | + return False |
| 111 | + |
| 112 | + |
| 113 | +# Main |
| 114 | +if __name__ == "__main__": |
| 115 | + log("Starting Quay User Automator") |
| 116 | + |
| 117 | + wait_for_quay() |
| 118 | + |
| 119 | + while True: |
| 120 | + if create_user(): |
| 121 | + sys.exit(0) |
| 122 | + |
| 123 | + log("Retrying user creation in 10s...") |
| 124 | + time.sleep(10) |
0 commit comments