config rewrite to fix oidc issues

This commit is contained in:
cortex
2026-05-21 07:13:08 +01:00
parent 790782fada
commit 5606d8083e
2 changed files with 47 additions and 6 deletions
+24 -2
View File
@@ -17,7 +17,7 @@ log_info "Initializing PostgreSQL..."
docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d --wait matrix-db docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d --wait matrix-db
# Wait for database readiness (silent except on failure) # Wait for database readiness (silent except on failure)
for ATTEMPT in $(seq 1 30); do for ATTEMPT in $(seq 1 60); do
if docker compose -f "$COMPOSE_FILE" exec -T matrix-db psql -U "$PG_USER" -c "SELECT 1;" > /dev/null 2>&1; then if docker compose -f "$COMPOSE_FILE" exec -T matrix-db psql -U "$PG_USER" -c "SELECT 1;" > /dev/null 2>&1; then
break break
fi fi
@@ -44,7 +44,7 @@ SELECT CASE WHEN NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'synapse'
EOF EOF
) )
echo "$INIT_SQL" | docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" exec -T matrix-db psql -U "$PG_USER" -f - echo "$INIT_SQL" | docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" exec -T matrix-db psql -U "$PG_USER" -f - > /dev/null
log_info "Generating Synapse configuration..." log_info "Generating Synapse configuration..."
docker run -it --rm --network matrix_network \ docker run -it --rm --network matrix_network \
@@ -53,13 +53,35 @@ docker run -it --rm --network matrix_network \
-e SYNAPSE_REPORT_STATS=no \ -e SYNAPSE_REPORT_STATS=no \
ghcr.io/element-hq/synapse:latest generate || { log_error "Synapse config generation failed"; exit 1; } ghcr.io/element-hq/synapse:latest generate || { log_error "Synapse config generation failed"; exit 1; }
SYNAPSE_CFG="${VOLUME_PATH}/synapse/homeserver.yaml"
yq -y -i '.database.name = "psycopg2"' "$SYNAPSE_CFG"
yq -y -i ".database.args.user = \"${PG_USER_MATRIX}\"" "$SYNAPSE_CFG"
yq -y -i ".database.args.password = \"${PG_PASSWORD_MATRIX}\"" "$SYNAPSE_CFG"
yq -y -i ".database.args.database = \"${PG_DB_MATRIX}\"" "$SYNAPSE_CFG"
yq -y -i ".database.args.host = \"matrix-db\"" "$SYNAPSE_CFG"
yq -y -i ".database.args.port = 5432" "$SYNAPSE_CFG"
yq -y -i ".database.args.sslmode = \"prefer\"" "$SYNAPSE_CFG"
yq -y -i ".public_baseurl = \"https://${HOMESERVER_FQDN}\"" "$SYNAPSE_CFG"
yq -y -i ".server_name = \"${HOMESERVER_FQDN}\"" "$SYNAPSE_CFG"
fix_ownership "${VOLUME_PATH}/synapse" fix_ownership "${VOLUME_PATH}/synapse"
log_info "Generating MAS configuration..." log_info "Generating MAS configuration..."
docker run -it --rm --network matrix_network \ docker run -it --rm --network matrix_network \
-v "${VOLUME_PATH}/mas:/data" \ -v "${VOLUME_PATH}/mas:/data" \
ghcr.io/element-hq/matrix-authentication-service:latest config generate \ ghcr.io/element-hq/matrix-authentication-service:latest config generate \
| grep -v '^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' > "${VOLUME_PATH}/mas/config.yaml" || { log_error "Mas config generation failed"; exit 1; } | grep -v '^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' > "${VOLUME_PATH}/mas/config.yaml" || { log_error "Mas config generation failed"; exit 1; }
MAS_CFG="${VOLUME_PATH}/mas/config.yaml"
# Ensure 'clients' array exists (prevents errors if we try to append later)
if ! yq '.clients' "$MAS_CFG" > /dev/null 2>&1; then
yq -y -i '.clients = []' "$MAS_CFG"
fi
yq -y -i ".public_base_url = \"https://${MAS_FQDN}\"" "$MAS_CFG"
yq -y -i ".http.listen = \"0.0.0.0:8000\"" "$MAS_CFG"
yq -y -i ".database.uri = \"postgres://${PG_USER_MAS}:${PG_PASSWORD_MAS}@matrix-db:5432/auth?sslmode=prefer\"" "$MAS_CFG"
fix_ownership "${VOLUME_PATH}/mas" fix_ownership "${VOLUME_PATH}/mas"
log_info "Configuration complete" log_info "Configuration complete"
+23 -4
View File
@@ -22,13 +22,32 @@ TEMP_FILTER=$(mktemp)
trap "rm -f $TEMP_FILTER" EXIT trap "rm -f $TEMP_FILTER" EXIT
cat > "$TEMP_FILTER" <<EOF cat > "$TEMP_FILTER" <<EOF
.oidc.providers += [{"id": "synapse", "client_id": "${CLIENT_ID}", "client_secret": "${CLIENT_SECRET}", "scopes": ["openid", "profile", "email"], "issuer": "http://mas:8000"}] clients:
- client_id: "${CLIENT_ID}"
client_secret: "${CLIENT_SECRET}"
redirect_uris:
- "https://${HOMESERVER_FQDN}/_synapse/client/oidc/callback"
grant_types:
- "authorization_code"
- "refresh_token"
response_types:
- "code"
scopes:
- "openid"
- "profile"
- "email"
application_type: "web"
EOF EOF
yq -y -i "$(cat "$TEMP_FILTER")" "$MAS_CFG" || { log_error "Failed to update MAS config"; exit 1; } EXISTING_CLIENT=$(yq '.clients[] | select(.client_id == "'"${CLIENT_ID}"'")' "$MAS_CFG" 2>/dev/null || echo "")
CLIENT_ID=$(yq '.oidc.providers[0].client_id' "$MAS_CFG" | tr -d '"') if [[ -z "$EXISTING_CLIENT" ]]; then
CLIENT_SECRET=$(yq '.oidc.providers[0].client_secret' "$MAS_CFG" | tr -d '"') # Append new client to MAS config
yq -y -i "$(cat "$TEMP_FILTER")" "$MAS_CFG" || { log_error "Failed to register Synapse client in MAS"; exit 1; }
log_info " -> Registered Synapse as OIDC client in MAS"
else
log_info " -> Synapse client already registered in MAS, skipping"
fi
ISSUER_URL="https://${MAS_FQDN}" ISSUER_URL="https://${MAS_FQDN}"