2026-01-08 05:12:14 -03:00
|
|
|
# services/comms/mas-local-users-ensure-job.yaml
|
|
|
|
|
apiVersion: batch/v1
|
|
|
|
|
kind: Job
|
|
|
|
|
metadata:
|
2026-01-14 14:17:26 -03:00
|
|
|
name: mas-local-users-ensure-8
|
2026-01-08 05:12:14 -03:00
|
|
|
namespace: comms
|
|
|
|
|
spec:
|
|
|
|
|
backoffLimit: 1
|
|
|
|
|
ttlSecondsAfterFinished: 3600
|
|
|
|
|
template:
|
2026-01-14 14:17:26 -03:00
|
|
|
metadata:
|
|
|
|
|
annotations:
|
|
|
|
|
vault.hashicorp.com/agent-inject: "true"
|
|
|
|
|
vault.hashicorp.com/role: "comms"
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-turn-shared-secret__TURN_STATIC_AUTH_SECRET: "kv/data/atlas/comms/turn-shared-secret"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-turn-shared-secret__TURN_STATIC_AUTH_SECRET: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/turn-shared-secret" -}}{{ .Data.data.TURN_STATIC_AUTH_SECRET }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-livekit-api__primary: "kv/data/atlas/comms/livekit-api"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-livekit-api__primary: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/livekit-api" -}}{{ .Data.data.primary }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-atlasbot-credentials-runtime__bot-password: "kv/data/atlas/comms/atlasbot-credentials-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-atlasbot-credentials-runtime__bot-password: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/atlasbot-credentials-runtime" -}}{{ index .Data.data "bot-password" }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-atlasbot-credentials-runtime__seeder-password: "kv/data/atlas/comms/atlasbot-credentials-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-atlasbot-credentials-runtime__seeder-password: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/atlasbot-credentials-runtime" -}}{{ index .Data.data "seeder-password" }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-chat-ai-keys-runtime__matrix: "kv/data/atlas/shared/chat-ai-keys-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-chat-ai-keys-runtime__matrix: |
|
|
|
|
|
{{- with secret "kv/data/atlas/shared/chat-ai-keys-runtime" -}}{{ .Data.data.matrix }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-chat-ai-keys-runtime__homepage: "kv/data/atlas/shared/chat-ai-keys-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-chat-ai-keys-runtime__homepage: |
|
|
|
|
|
{{- with secret "kv/data/atlas/shared/chat-ai-keys-runtime" -}}{{ .Data.data.homepage }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-mas-admin-client-runtime__client_secret: "kv/data/atlas/comms/mas-admin-client-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-mas-admin-client-runtime__client_secret: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/mas-admin-client-runtime" -}}{{ .Data.data.client_secret }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-synapse-db__POSTGRES_PASSWORD: "kv/data/atlas/comms/synapse-db"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-synapse-db__POSTGRES_PASSWORD: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/synapse-db" -}}{{ .Data.data.POSTGRES_PASSWORD }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-mas-db__password: "kv/data/atlas/comms/mas-db"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-mas-db__password: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/mas-db" -}}{{ .Data.data.password }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-mas-secrets-runtime__matrix_shared_secret: "kv/data/atlas/comms/mas-secrets-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-mas-secrets-runtime__matrix_shared_secret: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/mas-secrets-runtime" -}}{{ .Data.data.matrix_shared_secret }}{{- end -}}
|
|
|
|
|
vault.hashicorp.com/agent-inject-secret-mas-secrets-runtime__keycloak_client_secret: "kv/data/atlas/comms/mas-secrets-runtime"
|
|
|
|
|
vault.hashicorp.com/agent-inject-template-mas-secrets-runtime__keycloak_client_secret: |
|
|
|
|
|
{{- with secret "kv/data/atlas/comms/mas-secrets-runtime" -}}{{ .Data.data.keycloak_client_secret }}{{- end -}}
|
2026-01-08 05:12:14 -03:00
|
|
|
spec:
|
|
|
|
|
restartPolicy: Never
|
2026-01-14 05:07:23 -03:00
|
|
|
serviceAccountName: comms-vault
|
2026-01-08 05:12:14 -03:00
|
|
|
volumes:
|
2026-01-14 05:07:23 -03:00
|
|
|
- name: vault-scripts
|
|
|
|
|
configMap:
|
|
|
|
|
name: comms-vault-env
|
|
|
|
|
defaultMode: 0555
|
2026-01-08 05:12:14 -03:00
|
|
|
containers:
|
|
|
|
|
- name: ensure
|
|
|
|
|
image: python:3.11-slim
|
|
|
|
|
volumeMounts:
|
2026-01-14 05:07:23 -03:00
|
|
|
- name: vault-scripts
|
|
|
|
|
mountPath: /vault/scripts
|
2026-01-08 05:12:14 -03:00
|
|
|
readOnly: true
|
|
|
|
|
env:
|
|
|
|
|
- name: MAS_ADMIN_CLIENT_ID
|
|
|
|
|
value: 01KDXMVQBQ5JNY6SEJPZW6Z8BM
|
|
|
|
|
- name: MAS_ADMIN_CLIENT_SECRET_FILE
|
2026-01-14 05:07:23 -03:00
|
|
|
value: /vault/secrets/mas-admin-client-runtime__client_secret
|
2026-01-08 05:12:14 -03:00
|
|
|
- name: MAS_TOKEN_URL
|
|
|
|
|
value: http://matrix-authentication-service:8080/oauth2/token
|
|
|
|
|
- name: MAS_ADMIN_API_BASE
|
|
|
|
|
value: http://matrix-authentication-service:8081/api/admin/v1
|
|
|
|
|
- name: SEEDER_USER
|
|
|
|
|
value: othrys-seeder
|
|
|
|
|
- name: BOT_USER
|
|
|
|
|
value: atlasbot
|
|
|
|
|
command:
|
|
|
|
|
- /bin/sh
|
|
|
|
|
- -c
|
|
|
|
|
- |
|
|
|
|
|
set -euo pipefail
|
2026-01-14 05:07:23 -03:00
|
|
|
. /vault/scripts/comms_vault_env.sh
|
2026-01-08 05:12:14 -03:00
|
|
|
pip install --no-cache-dir requests >/dev/null
|
|
|
|
|
python - <<'PY'
|
|
|
|
|
import base64
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import requests
|
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
|
|
MAS_ADMIN_CLIENT_ID = os.environ["MAS_ADMIN_CLIENT_ID"]
|
|
|
|
|
MAS_ADMIN_CLIENT_SECRET_FILE = os.environ["MAS_ADMIN_CLIENT_SECRET_FILE"]
|
|
|
|
|
MAS_TOKEN_URL = os.environ["MAS_TOKEN_URL"]
|
|
|
|
|
MAS_ADMIN_API_BASE = os.environ["MAS_ADMIN_API_BASE"].rstrip("/")
|
2026-01-08 05:21:30 -03:00
|
|
|
AUTH_BASE = "http://matrix-authentication-service:8080"
|
2026-01-08 06:05:20 -03:00
|
|
|
SERVER_NAME = "live.bstein.dev"
|
2026-01-08 05:12:14 -03:00
|
|
|
|
|
|
|
|
def admin_token():
|
|
|
|
|
with open(MAS_ADMIN_CLIENT_SECRET_FILE, "r", encoding="utf-8") as f:
|
|
|
|
|
secret = f.read().strip()
|
|
|
|
|
basic = base64.b64encode(f"{MAS_ADMIN_CLIENT_ID}:{secret}".encode()).decode()
|
|
|
|
|
last = None
|
|
|
|
|
for attempt in range(1, 6):
|
|
|
|
|
try:
|
|
|
|
|
r = requests.post(
|
|
|
|
|
MAS_TOKEN_URL,
|
|
|
|
|
headers={"Authorization": f"Basic {basic}"},
|
|
|
|
|
data={"grant_type": "client_credentials", "scope": "urn:mas:admin"},
|
|
|
|
|
timeout=30,
|
|
|
|
|
)
|
|
|
|
|
if r.status_code == 200:
|
|
|
|
|
return r.json()["access_token"]
|
|
|
|
|
except Exception as exc: # noqa: BLE001
|
|
|
|
|
last = exc
|
|
|
|
|
time.sleep(attempt * 2)
|
|
|
|
|
raise RuntimeError(f"MAS admin token request failed: {last}")
|
|
|
|
|
|
|
|
|
|
def get_user(token, username):
|
|
|
|
|
r = requests.get(
|
|
|
|
|
f"{MAS_ADMIN_API_BASE}/users/by-username/{urllib.parse.quote(username)}",
|
|
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
|
|
|
timeout=30,
|
|
|
|
|
)
|
|
|
|
|
if r.status_code == 404:
|
|
|
|
|
return None
|
|
|
|
|
r.raise_for_status()
|
|
|
|
|
return r.json()["data"]
|
|
|
|
|
|
|
|
|
|
def create_user(token, username, password):
|
2026-01-08 05:21:30 -03:00
|
|
|
payloads = [
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"type": "user",
|
|
|
|
|
"attributes": {
|
|
|
|
|
"username": username,
|
|
|
|
|
"password": password,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{"username": username, "password": password},
|
|
|
|
|
]
|
|
|
|
|
for payload in payloads:
|
|
|
|
|
r = requests.post(
|
|
|
|
|
f"{MAS_ADMIN_API_BASE}/users",
|
|
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
|
|
|
json=payload,
|
|
|
|
|
timeout=30,
|
|
|
|
|
)
|
|
|
|
|
if r.status_code in (200, 201):
|
|
|
|
|
return r.json().get("data") or {}
|
|
|
|
|
if r.status_code == 409:
|
|
|
|
|
return None
|
2026-01-08 05:12:14 -03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def update_password(token, user_id, password):
|
2026-01-08 05:21:30 -03:00
|
|
|
r = requests.post(
|
2026-01-08 06:01:45 -03:00
|
|
|
f"{MAS_ADMIN_API_BASE}/users/{urllib.parse.quote(user_id)}/set-password",
|
2026-01-08 05:12:14 -03:00
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
2026-01-08 05:21:30 -03:00
|
|
|
json={"password": password},
|
2026-01-08 05:12:14 -03:00
|
|
|
timeout=30,
|
|
|
|
|
)
|
2026-01-08 05:21:30 -03:00
|
|
|
return r.status_code in (200, 204)
|
2026-01-08 05:12:14 -03:00
|
|
|
|
|
|
|
|
def ensure_user(token, username, password):
|
|
|
|
|
user = get_user(token, username)
|
|
|
|
|
if user is None:
|
|
|
|
|
user = create_user(token, username, password)
|
2026-01-08 05:21:30 -03:00
|
|
|
user = get_user(token, username)
|
2026-01-08 05:12:14 -03:00
|
|
|
if user is None:
|
|
|
|
|
raise RuntimeError(f"failed to ensure user {username}")
|
|
|
|
|
update_password(token, user["id"], password)
|
2026-01-08 06:05:20 -03:00
|
|
|
login_name = username
|
|
|
|
|
if not login_name.startswith("@"):
|
|
|
|
|
login_name = f"@{login_name}:{SERVER_NAME}"
|
2026-01-08 05:21:30 -03:00
|
|
|
r = requests.post(
|
|
|
|
|
f"{AUTH_BASE}/_matrix/client/v3/login",
|
|
|
|
|
json={
|
|
|
|
|
"type": "m.login.password",
|
2026-01-08 06:05:20 -03:00
|
|
|
"identifier": {"type": "m.id.user", "user": login_name},
|
2026-01-08 05:21:30 -03:00
|
|
|
"password": password,
|
|
|
|
|
},
|
|
|
|
|
timeout=30,
|
|
|
|
|
)
|
|
|
|
|
if r.status_code != 200:
|
|
|
|
|
raise RuntimeError(f"login failed for {username}: {r.status_code} {r.text}")
|
2026-01-08 05:12:14 -03:00
|
|
|
|
|
|
|
|
token = admin_token()
|
|
|
|
|
ensure_user(token, os.environ["SEEDER_USER"], os.environ["SEEDER_PASS"])
|
|
|
|
|
ensure_user(token, os.environ["BOT_USER"], os.environ["BOT_PASS"])
|
|
|
|
|
PY
|