keycloak: switch realm job to kcadm
This commit is contained in:
parent
6a155a7a7a
commit
77beacec53
@ -2,7 +2,7 @@
|
|||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
kind: Job
|
kind: Job
|
||||||
metadata:
|
metadata:
|
||||||
name: keycloak-realm-settings-3
|
name: keycloak-realm-settings-4
|
||||||
namespace: sso
|
namespace: sso
|
||||||
spec:
|
spec:
|
||||||
backoffLimit: 2
|
backoffLimit: 2
|
||||||
@ -21,9 +21,9 @@ spec:
|
|||||||
restartPolicy: OnFailure
|
restartPolicy: OnFailure
|
||||||
containers:
|
containers:
|
||||||
- name: configure
|
- name: configure
|
||||||
image: python:3.11-alpine
|
image: quay.io/keycloak/keycloak:26.0.7
|
||||||
env:
|
env:
|
||||||
- name: KEYCLOAK_URL
|
- name: KEYCLOAK_SERVER
|
||||||
value: http://keycloak.sso.svc.cluster.local
|
value: http://keycloak.sso.svc.cluster.local
|
||||||
- name: KEYCLOAK_REALM
|
- name: KEYCLOAK_REALM
|
||||||
value: atlas
|
value: atlas
|
||||||
@ -53,99 +53,19 @@ spec:
|
|||||||
args:
|
args:
|
||||||
- |
|
- |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
python - <<'PY'
|
/opt/keycloak/bin/kcadm.sh config credentials \
|
||||||
import json
|
--server "${KEYCLOAK_SERVER}" \
|
||||||
import os
|
--realm master \
|
||||||
import time
|
--user "${KEYCLOAK_ADMIN_USER}" \
|
||||||
import urllib.error
|
--password "${KEYCLOAK_ADMIN_PASSWORD}"
|
||||||
import urllib.parse
|
/opt/keycloak/bin/kcadm.sh update "realms/${KEYCLOAK_REALM}" \
|
||||||
import urllib.request
|
-s resetPasswordAllowed=true \
|
||||||
|
-s "smtpServer.host=${KEYCLOAK_SMTP_HOST}" \
|
||||||
base_url = os.environ["KEYCLOAK_URL"].rstrip("/")
|
-s "smtpServer.port=${KEYCLOAK_SMTP_PORT}" \
|
||||||
realm = os.environ["KEYCLOAK_REALM"]
|
-s "smtpServer.from=${KEYCLOAK_SMTP_FROM}" \
|
||||||
admin_user = os.environ["KEYCLOAK_ADMIN_USER"]
|
-s "smtpServer.fromDisplayName=${KEYCLOAK_SMTP_FROM_NAME}" \
|
||||||
admin_password = os.environ["KEYCLOAK_ADMIN_PASSWORD"]
|
-s "smtpServer.replyTo=${KEYCLOAK_SMTP_REPLY_TO}" \
|
||||||
|
-s "smtpServer.replyToDisplayName=${KEYCLOAK_SMTP_REPLY_TO_NAME}" \
|
||||||
smtp_defaults = {
|
-s smtpServer.auth=false \
|
||||||
"host": os.environ["KEYCLOAK_SMTP_HOST"],
|
-s smtpServer.starttls=false \
|
||||||
"port": os.environ["KEYCLOAK_SMTP_PORT"],
|
-s smtpServer.ssl=false
|
||||||
"from": os.environ["KEYCLOAK_SMTP_FROM"],
|
|
||||||
"fromDisplayName": os.environ["KEYCLOAK_SMTP_FROM_NAME"],
|
|
||||||
"replyTo": os.environ["KEYCLOAK_SMTP_REPLY_TO"],
|
|
||||||
"replyToDisplayName": os.environ["KEYCLOAK_SMTP_REPLY_TO_NAME"],
|
|
||||||
"auth": "false",
|
|
||||||
"starttls": "false",
|
|
||||||
"ssl": "false",
|
|
||||||
}
|
|
||||||
|
|
||||||
def request(path, method="GET", data=None, headers=None):
|
|
||||||
if headers is None:
|
|
||||||
headers = {}
|
|
||||||
payload = None
|
|
||||||
if data is not None:
|
|
||||||
payload = json.dumps(data).encode()
|
|
||||||
headers = dict(headers)
|
|
||||||
headers["Content-Type"] = "application/json"
|
|
||||||
req = urllib.request.Request(
|
|
||||||
f"{base_url}{path}",
|
|
||||||
data=payload,
|
|
||||||
headers=headers,
|
|
||||||
method=method,
|
|
||||||
)
|
|
||||||
return urllib.request.urlopen(req, timeout=10)
|
|
||||||
|
|
||||||
for _ in range(60):
|
|
||||||
try:
|
|
||||||
with request("/health/ready") as resp:
|
|
||||||
if resp.status == 200:
|
|
||||||
break
|
|
||||||
except Exception:
|
|
||||||
time.sleep(2)
|
|
||||||
else:
|
|
||||||
raise SystemExit("Keycloak API did not become ready in time")
|
|
||||||
|
|
||||||
token_data = urllib.parse.urlencode(
|
|
||||||
{
|
|
||||||
"grant_type": "password",
|
|
||||||
"client_id": "admin-cli",
|
|
||||||
"username": admin_user,
|
|
||||||
"password": admin_password,
|
|
||||||
}
|
|
||||||
).encode()
|
|
||||||
token_req = urllib.request.Request(
|
|
||||||
f"{base_url}/realms/master/protocol/openid-connect/token",
|
|
||||||
data=token_data,
|
|
||||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
||||||
method="POST",
|
|
||||||
)
|
|
||||||
with urllib.request.urlopen(token_req, timeout=10) as resp:
|
|
||||||
token_body = json.loads(resp.read().decode())
|
|
||||||
access_token = token_body["access_token"]
|
|
||||||
auth_headers = {"Authorization": f"Bearer {access_token}"}
|
|
||||||
|
|
||||||
with request(f"/admin/realms/{realm}", headers=auth_headers) as resp:
|
|
||||||
realm_data = json.loads(resp.read().decode())
|
|
||||||
|
|
||||||
changed = False
|
|
||||||
if not realm_data.get("resetPasswordAllowed", False):
|
|
||||||
realm_data["resetPasswordAllowed"] = True
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
smtp = realm_data.get("smtpServer") or {}
|
|
||||||
if not smtp.get("host"):
|
|
||||||
smtp.update(smtp_defaults)
|
|
||||||
realm_data["smtpServer"] = smtp
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
if not changed:
|
|
||||||
raise SystemExit(0)
|
|
||||||
|
|
||||||
with request(
|
|
||||||
f"/admin/realms/{realm}",
|
|
||||||
method="PUT",
|
|
||||||
data=realm_data,
|
|
||||||
headers=auth_headers,
|
|
||||||
) as resp:
|
|
||||||
if resp.status not in (200, 204):
|
|
||||||
raise SystemExit(f"Unexpected response: {resp.status}")
|
|
||||||
PY
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user