keycloak: retry token exchange permissions job

This commit is contained in:
Brad Stein 2026-01-03 15:45:04 -03:00
parent df959ee17d
commit 1f2bddc7fe

View File

@ -2,10 +2,10 @@
apiVersion: batch/v1 apiVersion: batch/v1
kind: Job kind: Job
metadata: metadata:
name: keycloak-portal-e2e-token-exchange-permissions-1 name: keycloak-portal-e2e-token-exchange-permissions-2
namespace: sso namespace: sso
spec: spec:
backoffLimit: 0 backoffLimit: 6
template: template:
spec: spec:
restartPolicy: Never restartPolicy: Never
@ -39,6 +39,7 @@ spec:
import json import json
import os import os
import re import re
import time
import urllib.parse import urllib.parse
import urllib.error import urllib.error
import urllib.request import urllib.request
@ -79,6 +80,7 @@ spec:
return exc.code, {"raw": raw.decode(errors="replace")} return exc.code, {"raw": raw.decode(errors="replace")}
def get_admin_token() -> str: def get_admin_token() -> str:
last_error: str | None = None
token_data = urllib.parse.urlencode( token_data = urllib.parse.urlencode(
{ {
"grant_type": "password", "grant_type": "password",
@ -93,13 +95,24 @@ spec:
headers={"Content-Type": "application/x-www-form-urlencoded"}, headers={"Content-Type": "application/x-www-form-urlencoded"},
method="POST", method="POST",
) )
try: for attempt in range(1, 61):
with urllib.request.urlopen(req, timeout=15) as resp: try:
body = json.loads(resp.read().decode()) with urllib.request.urlopen(req, timeout=15) as resp:
except urllib.error.HTTPError as exc: body = json.loads(resp.read().decode())
raw = exc.read().decode(errors="replace") token = body.get("access_token")
raise SystemExit(f"Token request failed: status={exc.code} body={raw}") if isinstance(token, str) and token:
return body["access_token"] return token
last_error = "missing access_token"
except urllib.error.HTTPError as exc:
# Treat transient startup errors as retryable.
if exc.code in (404, 429, 500, 502, 503, 504):
last_error = f"http {exc.code}"
else:
raise SystemExit(f"Token request failed: status={exc.code}")
except urllib.error.URLError as exc:
last_error = str(exc.reason)
time.sleep(2)
raise SystemExit(f"Token request failed after retries: {last_error}")
def find_client_uuid(token: str, client_id: str) -> str: def find_client_uuid(token: str, client_id: str) -> str:
status, clients = http_json( status, clients = http_json(